$(function () {
    //新建分组
    $("#confirmName").click(function () {
        var groupName = $(".groupName").val();//分组名
        var userIdss = chooseList;//所选用户id
        var openId = $("#openid").val();//创建者
        var params = {groupName: groupName, userIdss: userIdss, openId: openId};//参数
        $.ajax({
            url: "/web/sendMsg/createGrouping",
            type: "POST",
            dataType: "json", //返回数据形式为json
            contentType: 'application/json;charset=UTF-8',
            data: JSON.stringify(params),
            success: function (res) {
                if (res.code == "200") {
                    $('#myModal').modal('hide');
                    //重新请求结构
                    requestUser();
                    console.log(res)
                }
            }
        });
    });

    var tels = new Array();
    //下一步
    $(".next-step").click(function () {
        $(".two").addClass("active").siblings().removeClass("active");
        $(".editor").css("display", "block").siblings().css("display", "none");
        tels = [];
        $("#rightCont .to__item .to__name").each(function (key, value) {
            tels[key] = $(this).html();
        });
        $("#select-multiple").html("<select id=\"inform\" class=\"form-control\" multiple=\"multiple\"></select>");
        var option = "";
        for (var i = 0; i < tels.length; i++) {
            var item = tels[i];//用户名
            var id = chooseList[i];//用户id
            option += '\
           <option value="' + id + '" selected>' + item + '</option>\
            ';
        }
        $("#inform").html(option);
        $('#inform').fSelect();
        console.log(tels);
        console.log(chooseList);


    });
    //联系人下拉框是否全选
    $('input:radio').change(function () {
        $("#select-multiple").html("<select id=\"inform\" class=\"form-control\" multiple=\"multiple\"></select>");
        var val=$('input:radio:checked').val();
        if(val === "true"){
            var option = "";
            for (var i = 0; i < tels.length; i++) {
                var item = tels[i];//用户名
                var id = chooseList[i];//用户id
                option += '\
           <option value="' + id + '" selected>' + item + '</option>\
            ';
            }
            $("#inform").html(option);
            $('#inform').fSelect();
        }else if(val === "false"){
            var option = "";
            for (var i = 0; i < tels.length; i++) {
                var item = tels[i];//用户名
                var id = chooseList[i];//用户id
                option += '\
           <option value="' + id + '">' + item + '</option>\
            ';
            }
            $("#inform").html(option);
            $('#inform').fSelect();
        }
    });

    //确认发送
    $(".confirm-send").click(function () {
        var userIdss = chooseList;//所选用户id
        var msgText = $("#contents").val();//内容
        var msgTitle = $("#titles").val();//标题
        var sender = $("#openid").val();//发送者
        var source = $("#source").val();//来源
        var ifSMS = $('input:radio:checked').val();//是否发送短信
        var shortUserIdss = $("#inform").val();//所选发送短信用户id
        var picture = imgUrl; //图片路径
        var paramsObj = {
            userIdss: userIdss,
            msgText: msgText,
            msgTitle: msgTitle,
            sender: sender,
            source: source,
            ifSMS: ifSMS,
            picture: picture,
            shortUserIdss:shortUserIdss
        };
        debugger;
        //空值检验判断
        if (msgText === "" || msgTitle === "" || source === "") {
            alert("输入框不能为空，请重新输入");
        } else {
            $.ajax({
                url: "/web/sendMsg/addAppMsg",
                type: "POST",
                dataType: "json", //返回数据形式为json
                contentType: 'application/json;charset=UTF-8',
                data: JSON.stringify(paramsObj),
                success: function (res) {
                    if (res.code == "200") {
                        alert(res.message);
                        restForm();
                    } else {
                        alert(res.message);
                    }
                }
            });
        }


    });

    //返回
    $(".sendSuccess strong").click(function () {
        $(".one").addClass("active").siblings().removeClass("active");
        $(".selectContacts").css("display", "block").siblings().css("display", "none");
    });
    //返回编辑页
    $(".back-editor").click(function () {
        $(".selectContacts").css("display", "block").siblings().css("display", "none");
    });
    /**
     * 上传图片
     */
        //确认上传图片
    var imgUrl = "";//上传的图片路径
    $("#btn-upload").click(function () {
        $.ajaxFileUpload({
            type: "post",
            fileElementId: "fileupload",
            secureuri: false,
            dataType: 'json',
            url: "/web/sendMsg/uploadPicture",
            async: false,
            success: function (data) {
                if (data.code == "200") {
                    imgUrl = data.data.picture;
                    $("#fileImg").attr("src", imgUrl);
                    $("#fileImg").css("display", "block");
                    alert('上传成功');
                } else {
                    alert('上传失败');
                }
            },
            error: function () {
                alert("佐证材料上传失败 ！！请联系管理员");
            }
        });
    });


    //发送成功重置表单
    function restForm() {
        chooseList = [];//所选用户id
        $("#contents").val("");//内容
        $("#titles").val("");//标题
        $("#source").val("");//来源
        imgUrl = ""; //图片路径
        //跳转页面
        $(".three").addClass("active").siblings().removeClass("active");
        $(".sendSuccess").css("display", "block").siblings().css("display", "none");

        countdown();//倒计时
        //请求组织结构
        requestUser();
    }

    // 发送成功倒计时
    function countdown() {
        var t = 3;
        var time = document.getElementsByClassName("clock")[0];
        var inter = setInterval(function () {
            t--;
            time.innerHTML = t;
            if (t <= 0) {
                $(".one").addClass("active").siblings().removeClass("active");
                $(".selectContacts").css("display", "block").siblings().css("display", "none");
                clearInterval(inter);
                t = 3;
                $(".clock").html(3);
            }
        }, 1000);
    }

    // 是否选择自定义图片
    $("#msgType").change(function () {
        var type = $(this).val();
        debugger;
        if (type === "") {
            $(".diyImg").css("display", "block");
            imgUrl = "";
        } else {
            $(".diyImg").css("display", "none");
            imgUrl = type;
        }
    });
})

//请求组织结构
function requestUser() {
    var openId = $("#openid").val();//创建者
    var params = {openId: openId};//参数
    $.ajax({
        url: "/web/sendMsg/queryDeptTree",
        type: "POST",
        dataType: "json", //返回数据形式为json
        contentType: 'application/json;charset=UTF-8',
        data: JSON.stringify(params),
        success: function (res) {
            if (res.code == "200") {
                var baseData = res.data;
                console.log(baseData);
                result = new group(baseData);
                result.go();
                $(".to__subItem").removeClass("to__show");
            }
        }
    });
}

//使textarea标签支持缩进
$("textarea").on(
    'keydown',
    function (e) {
        if (e.keyCode == 9) {
            e.preventDefault();
            var indent = '    ';
            var start = this.selectionStart;
            var end = this.selectionEnd;
            var selected = window.getSelection().toString();
            selected = indent + selected.replace(/\n/g, '\n' + indent);
            this.value = this.value.substring(0, start) + selected
                + this.value.substring(end);
            this.setSelectionRange(start + indent.length, start
                + selected.length);
        }
    })
window.onload = function () {
    //请求组织结构
    requestUser();
}